package database.mybatis.c_xml_mapper;

import database.mybatis.c_xml_mapper.mapper.*;
import database.mybatis.entity.GeneratedInt;
import database.mybatis.entity.GeneratedStr;
import database.mybatis.entity.SysUser;
import database.utils.GUIDUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        testForeach();
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试if条件
     * @Date 15:43 2023/5/22
     * @Param []
     * @return void
     **/
    private static void testIf() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        SysUser sysUser = new SysUser();
        sysUser.setUserNo("dmi");
        sysUser.setUserName("");
        System.out.println(sysUserMapper.ifTest(sysUser));
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试foreach标签
     * @Date 17:18 2023/5/22
     * @Param []
     * @return void
     **/
    private static void testForeach() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        List<String> list = new ArrayList<>();
        list.add("admin");
//        list.add("wenlinjie1");
        System.out.println(sysUserMapper.testForeach(list));
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试choose（多个条件中匹配最先匹配到的添加，后面的不管）
     * @Date 16:06 2023/5/22
     * @Param []
     * @return void
     **/
    private static void chooseTest() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        SysUser sysUser = new SysUser();
        sysUser.setUserNo("");
        sysUser.setUserName("");
        System.out.println(sysUserMapper.chooseTest(sysUser));
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试where条件
     * @Date 16:31 2023/5/22
     * @Param []
     * @return void
     **/
    private static void whereTest() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        SysUser sysUser = new SysUser();
        sysUser.setUserNo("admin");
        sysUser.setUserName("");
        System.out.println(sysUserMapper.whereTest(sysUser));
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试trim，插入数据
     * @Date 16:32 2023/5/22
     * @Param []
     * @return void
     **/
    private static void testTrimInsert() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        GeneratedStr generatedStr = new GeneratedStr("2xa131", "a", 19);
        sysUserMapper.testTrimInsert(generatedStr);
        session.commit();
        session.close();
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试set标签更新
     * @Date 17:07 2023/5/22
     * @Param []
     * @return void
     **/
    private static void testSetUpdate() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        GeneratedStr generatedStr = new GeneratedStr("2xa131", "a1", null);
        sysUserMapper.testSetUpdate(generatedStr);
        session.commit();
        session.close();
    }
    /**
     * 测试返回非实体类型的dto
     * @throws IOException
     */
    private static void testDto() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        System.out.println(sysUserMapper.findDtoById("1"));
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试复杂结果集映射1
     * @Date 17:17 2023/4/3
     * @Param []
     * @return void
     **/
    private static void testComplicatedResultMap1() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        System.out.println("单表查询：" + sysUserMapper.findById("1"));
        System.out.println("多表查询：" + sysUserMapper.findResultMapTest("admin"));
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试简单结果集映射
     * @Date 17:06 2023/4/3
     * @Param []
     * @return void
     **/
    private static void testSimpleResultMap() throws IOException {
        SqlSession session = getSession();
        GeneratedIntMapper generatedIntMapper = session.getMapper(GeneratedIntMapper.class);
        System.out.println(generatedIntMapper.findByIdForResultMap(1));
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试参数SQL
     * @Date 15:42 2023/4/3
     * @Param []
     * @return void
     **/
    private static void testParamSql() throws IOException {
        SqlSession session = getSession();
        OrgDeptMapper orgDeptMapper = session.getMapper(OrgDeptMapper.class);
        System.out.println(orgDeptMapper.findByColumn("id", "1"));
        // 不安全
//        System.out.println(orgDeptMapper.findByColumn("1 = 1 OR id ", "1"));
    }
    /**
     * @Author WLJ
     * @Description //TODO include标签测试
     * @Date 15:41 2023/4/3
     * @Param []
     * @return void
     **/
    private static void testInclude() throws IOException {
        SqlSession session = getSession();
        OrgDeptMapper orgDeptMapper = session.getMapper(OrgDeptMapper.class);
        System.out.println(orgDeptMapper.findCloById("1"));
    }

    /**
     * @Author WLJ
     * @Description //TODO 测试int类型自动生成主键
     * @Date 15:22 2023/4/3
     * @Param []
     * @return void
     **/
    private static void testGeneratedIntTest() throws IOException {
        SqlSession sqlSession = getSession();
        GeneratedIntMapper generatedIntMapper = sqlSession.getMapper(GeneratedIntMapper.class);
        List<GeneratedInt> generatedIntList = Arrays.asList(new GeneratedInt("a", 1), new GeneratedInt("b", 2));
        /*for (int i = 0; i < generatedIntList.size(); i++) {
            generatedIntList.get(i).setId(i + 1);
        }*/
        generatedIntMapper.insertManyGenerated(generatedIntList);
        generatedIntMapper.insertOneGenerated(new GeneratedInt("c", 3));
        sqlSession.commit();
        sqlSession.close();
    }
    /**
     * @Author WLJ
     * @Description //TODO 测试string类型自动生成主键
     * @Date 15:22 2023/4/3
     * @Param []
     * @return void
     **/
    private static void testGeneratedStrTest() throws IOException {
        SqlSession sqlSession = getSession();
        GeneratedStrMapper generatedStrMapper = sqlSession.getMapper(GeneratedStrMapper.class);
        generatedStrMapper.insertOne(new GeneratedStr(GUIDUtil.getGUID(), "a", 1));
        // 字符串类型一般不自动生成id，后面集成spring或jsp能否实现
//        generatedStrMapper.insertOneGenerated(new GeneratedStr("b", 2));
        sqlSession.commit();
        sqlSession.close();
    }
    /**
     * @Author WLJ
     * @Description //TODO 获取Session
     * @Date 15:23 2023/4/3
     * @Param []
     * @return org.apache.ibatis.session.SqlSession
     **/
    private static SqlSession getSession() throws IOException {
        String configFilePath = "database.mybatis/c_xml_mapper/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(configFilePath);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory.openSession();
    }
}
